package com.controller
{
	import com.events.SubTablesEvent;
	import com.model.valueObjects.VOFolder;
	import com.service.InvoiceMasterDetails;
	import com.service.SubTablesDetails;
	import com.view.FilterSelector;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class SubTablesController extends AbstractController
	{
		import com.model.Model;
		private var isInlineEdit:Boolean=false;
		[Inject]
		public var subTablesDetails:SubTablesDetails;
		[Inject]
		public var invoiceMasterDetails:InvoiceMasterDetails;

		[EventHandler(event="SubTablesEvent.GET_ALL_FOLDER")]
		public function getAllFolder():void{
			
			sh.executeServiceCall(subTablesDetails.getAllFolders(model.currentDatabase), getAllFolderResult, handleFault );
		}
		
		private function getAllFolderResult(event:ResultEvent):void
		{
	
			model.FolderMaster=event.result as ArrayCollection;
			model.FolderMaster.refresh();
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="SubTablesEvent.GET_ALL_VENDOR")]
		public function getAllVendor():void{
			sh.executeServiceCall(subTablesDetails.getAllVendors(model.currentDatabase), getAllVendorResult, handleFault );
		}
		
		private function getAllVendorResult(event:ResultEvent):void
		{
			model.VendorMaster=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}

		[EventHandler(event="SubTablesEvent.GET_ALL_CATEGORY")]
		public function getAllCatagory():void{
			sh.executeServiceCall(subTablesDetails.getAllCatagory(model.currentDatabase), getAllCatagoryResult, handleFault );
		}
		
		private function getAllCatagoryResult(event:ResultEvent):void
		{
			model.CatagoryMaster=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="SubTablesEvent.GET_ALL_PROJECT_CODE")]
		public function getAllProjectCodes():void{
			sh.executeServiceCall(subTablesDetails.getAllProjectCode(model.currentDatabase), getAllProjectCodeResult, handleFault );
		}
		
		private function getAllProjectCodeResult(event:ResultEvent):void
		{
			model.ProjectCodeMaster=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="SubTablesEvent.GET_ALL_PAYMENT_TYPE")]
		public function getAllPaymentTypes():void{
			sh.executeServiceCall(subTablesDetails.getAllPaymentType(model.currentDatabase), getAllPaymentTypeResult, handleFault );
		}
		
		private function getAllPaymentTypeResult(event:ResultEvent):void
		{
			model.PaymentTypeMaster=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="SubTablesEvent.GET_ALL_EXPENSE_TYPE")]
		public function getAllExpenseTypes():void{
			sh.executeServiceCall(subTablesDetails.getAllExpenseType(model.currentDatabase), getAllExpenseTypeResult, handleFault );
		}
		
		private function getAllExpenseTypeResult(event:ResultEvent):void
		{
			model.ExpenseTypeMaster=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="SubTablesEvent.GET_ALL_LOCATION")]
		public function getAllLocations():void{
			sh.executeServiceCall(subTablesDetails.getAllLocation(model.currentDatabase), getAllLocationResult, handleFault );
		}
		
		private function getAllLocationResult(event:ResultEvent):void
		{
			model.LocationMaster=event.result as ArrayCollection;
			getAllInvoices();
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="SubTablesEvent.GET_ALL_CUSTOMER")]
		public function getAllCustomers():void{
			sh.executeServiceCall(subTablesDetails.getAllCustomer(model.currentDatabase), getAllCustomerResult, handleFault );
		}
		
		private function getAllCustomerResult(event:ResultEvent):void
		{
			model.CustomerMaster=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="SubTablesEvent.GET_ALL_TAX_CATEGORY")]
		public function getAllTaxCatagory():void{
			sh.executeServiceCall(subTablesDetails.getAllTaxCatagory(model.currentDatabase), getAllTaxCatagoryResult, handleFault );
		}
		
		private function getAllTaxCatagoryResult(event:ResultEvent):void
		{
			model.TaxCatagoryMaster=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="SubTablesEvent.GET_ALL_USER_ID")]
		public function getAllUserID():void{
			sh.executeServiceCall(subTablesDetails.getAllUserID(model.currentDatabase), getAllUserIDResult, handleFault );
		}
		
		private function getAllUserIDResult(event:ResultEvent):void
		{
			model.UserIDMaster=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="SubTablesEvent.ADD_NEW_OPTION", properties="table,item,description,invoiceNo")]
		public function inlineAddNewOption(table:String,item:String,description:String,invoiceNo:Number):void{
			Alert.show("pre inline");
			sh.executeServiceCall(subTablesDetails.addNewDescription(table,item,description,invoiceNo,model.currentDatabase),inlineAddNewOptionResult, handleFault );
		}
		
		private function inlineAddNewOptionResult(event:ResultEvent):void
		{
			isInlineEdit=true;
			model.InvoiceMasterDetails=event.result as ArrayCollection;
			getAllFolder();
			getAllVendor();
			getAllCatagory();
			getAllLocations();
			getAllCustomers();
			getAllUserID();
			getAllPaymentTypes();
			
		}
	
		private function getAllInvoices():void{
			if(isInlineEdit){
			isInlineEdit=false;
			var fs:FilterSelector = new FilterSelector();
			fs.filterField = "folder_id";
			fs.addFilterTypes(model.currentFolders);
			model.InvoiceMasterDetails.filterFunction = fs.generateFilterFunction();
			model.InvoiceMasterDetails.refresh(); 
			CursorManager.removeBusyCursor();
			}
		}
	
		private function handleFault(event:FaultEvent):void
		{
			//Alert.show( 'v '+event.fault.name, "Error" );
			CursorManager.removeBusyCursor();
		}
		
		
	}
}